package com.agu.mgt.fsmsmgt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.agu.common.annotation.SysLog;
import com.agu.common.excel.Column;
import com.agu.common.excel.ExcelExport;
import com.agu.entity.fms.FsmsSendCanvasEntity;
import com.agu.mgt.fms.service.FsmsSendCanvasService;
import com.agu.mgt.sys.controller.AbstractController;
import com.agu.result.Result;
import com.agu.result.ResultGenerator;
import com.agu.utils.CompareUtils;
import com.agu.utils.Query;
import com.agu.utils.xss.XssHttpServletRequestWrapper;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 发送区块表
 * 
 * @author 
 * @email 
 * @date 2019-01-22 10:37:10
 */
 @SuppressWarnings("rawtypes")
@RestController
@Api("发送区块表")
@RequestMapping("fsmssendcanvasmgt")
public class FsmsSendCanvasMgtController extends AbstractController{
	@Autowired
	private FsmsSendCanvasService fsmsSendCanvasService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@ApiOperation(value = "列表", notes = "说明")
	@RequiresPermissions("fsmssendcanvas:list")
	public Result list(@RequestBody Map params){

		// 查询列表数据
		Query query = new Query(params);
		Page<FsmsSendCanvasEntity> page = fsmsSendCanvasService.queryPageList(query);
		return ResultGenerator.successPage(page);
	}
	
	
	/**
	 * 信息
	 */
	@GetMapping("/info/{id}")
	@ApiOperation(value = "信息", notes = "说明")
	@RequiresPermissions("fsmssendcanvas:info")
	public Result info(@PathVariable("id") Integer id){
		FsmsSendCanvasEntity fsmsSendCanvas = fsmsSendCanvasService.queryObject(id);
		
		return ResultGenerator.successData(fsmsSendCanvas);
	}
	
	/**
	 * 保存
	 */
	@SysLog("新增发送区块表")
	@PostMapping("/save")
	@ApiOperation(value = "保存", notes = "说明")
	@RequiresPermissions("fsmssendcanvas:save")
	public Result save(@RequestBody FsmsSendCanvasEntity fsmsSendCanvas){
	
		fsmsSendCanvasService.save(fsmsSendCanvas);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("id","id");
		     cmap.put("sendId","发送id");
		     cmap.put("canvasId","区域ID");
		     cmap.put("canvasType","区域类别");
		     cmap.put("canvasSort","排序");
		     cmap.put("content","内容");
		     cmap.put("url","内容标识");
		     cmap.put("createTime","创建时间");
		String remark = CompareUtils.getObjectRemark(fsmsSendCanvas, cmap);
		return ResultGenerator.success("保存成功").setRemark(remark);
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改发送区块表")
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "说明")
	@RequiresPermissions("fsmssendcanvas:update")
	public Result update(@RequestBody FsmsSendCanvasEntity fsmsSendCanvas){
	
		FsmsSendCanvasEntity fsmsSendCanvas1 = fsmsSendCanvasService.queryObject(fsmsSendCanvas.getId());
	
		fsmsSendCanvasService.update(fsmsSendCanvas);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		
		     cmap.put("id","id");
		     cmap.put("sendId","发送id");
		     cmap.put("canvasId","区域ID");
		     cmap.put("canvasType","区域类别");
		     cmap.put("canvasSort","排序");
		     cmap.put("content","内容");
		     cmap.put("url","内容标识");
		     cmap.put("createTime","创建时间");
	
		String remark = CompareUtils.getTwoObjectRemark(fsmsSendCanvas1, fsmsSendCanvas, cmap);
	
		return ResultGenerator.success("修改成功").setRemark(remark);
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除发送区块表")
	@GetMapping("/delete/{id}")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("fsmssendcanvas:delete")
	public Result delete(@PathVariable("id") Integer id){
	
	    	if (id == null) {
			return ResultGenerator.fail("缺少参数");
		}
	
		FsmsSendCanvasEntity fsmsSendCanvas = fsmsSendCanvasService.queryObject(id);
	
		fsmsSendCanvasService.delete(id);
		
		//处理日志
		Map<String,String> cmap = new HashMap<String,String>();
		     cmap.put("id","id");
		     cmap.put("sendId","发送id");
		     cmap.put("canvasId","区域ID");
		     cmap.put("canvasType","区域类别");
		     cmap.put("canvasSort","排序");
		     cmap.put("content","内容");
		     cmap.put("url","内容标识");
		     cmap.put("createTime","创建时间");
		String remark = CompareUtils.getObjectRemark(fsmsSendCanvas, cmap);
		return ResultGenerator.success("删除成功").setRemark(remark);
	}
	
	/**
	 * 批量删除
	 */
	@SysLog("批量删除发送区块表")
	@PostMapping("/deletes")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("fsmssendcanvas:delete")
	public Result deletes(@RequestBody Integer[] ids){
	
		fsmsSendCanvasService.deleteBatch(ids);
		
		return ResultGenerator.success("删除成功");
	}
	
	@RequestMapping("/download")
	@ApiOperation(value = "下载", notes = "说明")
	@RequiresPermissions("fsmssendcanvas:list")
	public void download(HttpServletRequest request, HttpServletResponse response) {

		HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);
		
		String query = orgRequest.getParameter("query");

		HashMap querymap = JSON.parseObject(query, HashMap.class);

		List<FsmsSendCanvasEntity> list = fsmsSendCanvasService.queryList(querymap);

		if (list != null && list.size() > 0) {
			Map map = new HashMap();

               Map<Integer, String> canvasTypeMap = new HashMap<Integer, String>();

                canvasTypeMap.put(1, "视频");
                canvasTypeMap.put(2, "图片");
                canvasTypeMap.put(3, "文本");
            map.put("canvasTypeMap", canvasTypeMap);
               Map<String, String> urlMap = new HashMap<String, String>();

                urlMap.put("1", "模板提供");
                urlMap.put("2", "发送时提供");

            map.put("urlMap", urlMap);

			List<Column> clist = new ArrayList();
			Column cloumn = null;

            cloumn = new Column("id", "id", 1);
			clist.add(cloumn);
            cloumn = new Column("发送id", "sendId", 1);
			clist.add(cloumn);
            cloumn = new Column("区域ID", "canvasId", 1);
			clist.add(cloumn);
            cloumn = new Column("区域类别", "canvasType", 2);
			clist.add(cloumn);
            cloumn = new Column("排序", "canvasSort", 1);
			clist.add(cloumn);
            cloumn = new Column("内容", "content", 1);
			clist.add(cloumn);
            cloumn = new Column("内容标识", "url", 2);
			clist.add(cloumn);
            cloumn = new Column("创建时间", "createTime", 1);
			clist.add(cloumn);
			try {
				ExcelExport export = exportList(list, clist, map);
				if (export != null) {
					this.downExcel(export, response);
					//this.downzip(export, response);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
